/**************************************************************************//**
 * @item     CosyOS-III Demo
 * @file     demo_debug_hal.c
 * @brief    例程-DEBUG for STM32F4 with HAL Drivers
 * @author   迟凯峰
 * @version  V1.0.0
 * @date     2025.06.07
 ******************************************************************************/

#if 0 // 编译开关

#include "CosyOS.H"
#if SYSCFG_DEBUGGING == __ENABLED__

/*
 * 用户定义
 */

/*-------------------------------------------------------------*
 | DEBUG_MODE | 输出（UART SEND）| 输入（UART RECV）| 推荐等级 |
 |------------+------------------+------------------+----------|
 | 0          | printf（查询）   | 串口接收中断     | *        |
 |------------+------------------+------------------+----------|
 | 1          | 串口发送中断     | 串口接收中断     | ***      |
 |------------+------------------+------------------+----------|
 | 2          | DMA串口发送中断  | DMA串口接收中断  | *****    |
 *-------------------------------------------------------------*/
#define DEBUG_MODE          0

/* DEBUG RECV 缓存SIZE */
#define DEBUG_RECV_BUFF     32

////////////////////////////////////////////////////////////////////////////////

// 1、例程中使用了UART1做为DEBUG专用串口，用户如需更换其它串口请自行调整。
// 2、下方的DEBUG配置函数 Debug_Config()，不能够取代基本的串口初始化工作，
//    用户在main函数中，在调用该函数之前，需先行初始化串口。

////////////////////////////////////////////////////////////////////////////////

extern UART_HandleTypeDef huart1;
static char  u_debug_recv_buff[DEBUG_RECV_BUFF];
static char *u_debug_recv_p = u_debug_recv_buff;
#if DEBUG_MODE == 0
#include <stdio.h>
#endif

void Debug_Config(void)
{
    #if   DEBUG_MODE == 0
    HAL_UART_Receive_IT (&huart1, (uint8_t *)u_debug_recv_p, 1);
    
    #elif DEBUG_MODE == 1
    HAL_UART_Receive_IT (&huart1, (uint8_t *)u_debug_recv_p, 1);
    
    #elif DEBUG_MODE == 2
    HAL_UART_Receive_DMA(&huart1, (uint8_t *)u_debug_recv_p, 1);
    #endif
}

#if SYSCFG_DEBUG_SENDLEN == __DISABLED__
__WEAK void debug_hook(char _XDATA_MEM_ *p)
#else
__WEAK void debug_hook(char _XDATA_MEM_ *p, s_u16_t len)
#endif
{
    #if   DEBUG_MODE == 0
    printf("%s", p);
    xDebugSend_Complete();
    
    #elif DEBUG_MODE == 1
    HAL_UART_Transmit_IT (&huart1, (uint8_t *)p, len);
    
    #elif DEBUG_MODE == 2
    HAL_UART_Transmit_DMA(&huart1, (uint8_t *)p, len);
    #endif
}

#if DEBUG_MODE >= 1
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
    if(huart->Instance == USART1){
        xDebugSend_Complete();
    }
}
#endif

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    if(huart->Instance == USART1){
        if(u_debug_recv_p - u_debug_recv_buff < DEBUG_RECV_BUFF - 1){
            if(*u_debug_recv_p++ == '\n'){
                xDebugRecv_Handler(u_debug_recv_buff, u_debug_recv_p - u_debug_recv_buff);
                u_debug_recv_p = u_debug_recv_buff;
            }
        }
        else{
            u_debug_recv_p = u_debug_recv_buff;
        }
        #if   DEBUG_MODE == 0
        HAL_UART_Receive_IT (&huart1, (uint8_t *)u_debug_recv_p, 1);
        
        #elif DEBUG_MODE == 1
        HAL_UART_Receive_IT (&huart1, (uint8_t *)u_debug_recv_p, 1);
        
        #elif DEBUG_MODE == 2
        HAL_UART_Receive_DMA(&huart1, (uint8_t *)u_debug_recv_p, 1);
        #endif
    }
}

#endif

#endif
